package com.yidu.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.bean.TbManager;
import com.yidu.bean.TbModule;
import com.yidu.bean.TbRole;
import com.yidu.bean.TbRoleModule;
import com.yidu.bean.TbType;
import com.yidu.commom.Tool;
import com.yidu.message.Menu;
import com.yidu.message.Message;
import com.yidu.message.Pages;
import com.yidu.service.TbModuleService;
import com.yidu.service.TbRoleModuleService;

/**
 * 类的描述-- 模块
 * @author 楠竹
 * @date 2018年7月23日
 * @version 1.0
 */
@Controller
@RequestMapping("/module")
public class TbModuleAction {

	//控制台打印输出语句
	Logger logger = Logger.getLogger(TbModuleAction.class);

	//消息消息类
	Message msg = new Message();

	//json
	ObjectMapper mapper = new ObjectMapper();
	
	
	//icon
	String[] attr = {"icon-zhuti","icon-database","icon-lanmuguanli","icon-yonghu1","icon-zhandianpeizhi","icon-yonghu1","icon-zhandianpeizhi"};
	 
	
	@Resource
	private TbModuleService moduleService;
	
	@Resource
	private TbRoleModuleService roleModuleService;
	
	
	
	@RequestMapping("/findMod")
	@ResponseBody
	private Object findMod(HttpSession session) throws JsonProcessingException{
		
		logger.info("进入查询用户模块...");
		
		TbManager manager  = (TbManager) session.getAttribute("manager");
		
		//祖父集合
		List<Object> listObj = new ArrayList<>();
		
		//当session不为空
		if(!StringUtils.isEmpty(manager)){
			//得到模块
			List<TbModule> list = moduleService.findMod(manager.getManId());
			
			logger.info(mapper.writeValueAsString(list));
			//得到菜单格式 子集合
			List<Menu> listMenu = new ArrayList<>();
			
			 for (int i = 0; i < list.size(); i++) {
				TbModule mod = list.get(i);
				
				//查询 是拥有下级模块
				List<TbModule> listMod = moduleService.findUp(mod.getModId());
				
				logger.info(mapper.writeValueAsString(listMod));
				//定义一个 menu集合
				List<Menu> listFu = new ArrayList<>();
				for (int j = 0; j < listMod.size(); j++) {
					TbModule mods = listMod.get(j);
					logger.info(mapper.writeValueAsString(mods));
					
					Menu mu = new Menu();
					mu.setIcon(attr[i]);
					mu.setUrl(mods.getUrl());
					mu.setName(mods.getModName());
					mu.setStrClass("iconfont"+" "+attr[i]);
					
					listFu.add(mu);
				}
				 
				//填充菜单模型
				Menu menu = new Menu();
				menu.setIcon(attr[i]);
				menu.setUrl(mod.getUrl());
				menu.setName(mod.getModName());
				menu.setStrClass("iconfont"+" "+attr[i]);
				menu.setList(listFu);
				
				//加入菜单集合
				listMenu.add(menu);
			}
			 
			 listObj.add(manager);
			 listObj.add(listMenu);
			 
		}
		logger.info(mapper.writeValueAsString(listObj));
		 return listObj;
		  
		
		
	}
	@ResponseBody
	@RequestMapping("/showUpdate")
	private Object showUpdate(String modId){
		logger.info("进入根据ID查询...");
		TbModule mod = moduleService.findById(modId);
		
		return mod;
		
	}
	@ResponseBody
	@RequestMapping("/delete")
	private  Object delete(String modId){
		logger.info("进入删除...");
		int num =moduleService.delete(modId);
		
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		
		return msg;
	}
	
	@ResponseBody
	@RequestMapping("/saveOrUpdate")
	private Object saveOrUpdate(TbModule mod) throws JsonProcessingException{
		logger.info("进入增加或修改...");
		logger.info(mapper.writeValueAsString(mod));
		
		//根据id判断 增加 修改
		int num = 0;
		if(!Tool.isEmpty(mod.getModId())){
			//不为空 修改
			num = moduleService.update(mod);
		}else{
			//反之 增加
			mod.setModId(Tool.getRandomString(32));
			num = moduleService.save(mod);
		}
		
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		
		return msg;
	}
	
	 
	
	@RequestMapping("/findVague")
	@ResponseBody
	public Object findVague(HttpServletRequest request,Pages pages,TbModule mod) throws IOException{
		logger.info("进入模糊查询...");


		logger.info(mapper.writeValueAsString(pages));
		//得到查询结果集
		List<TbModule> list = moduleService.findVague(pages, mod);

		 
		logger.info(mapper.writeValueAsString(list));

		//类型转换
		Long lon= (long) moduleService.findByCount(mod);
		int row = lon.intValue();


		//由于前端所需条件
		Map<String, Object> map = new HashMap<>();

		map.put("code", 0);
		map.put("msg","");
		map.put("count",row);
		map.put("data", list);
		return map;
	}
	
	@RequestMapping("/findAll")
	@ResponseBody
	private Object findAll(HttpSession session){
		
		logger.info("进入查询所有...");
		
		List<Object> lists  = new ArrayList<>();
		
		List<TbModule> list = moduleService.findAll();
		
		TbRole role = (TbRole) session.getAttribute("showRole");
		
		lists.add(role);
		lists.add(list);
		
		return lists;
				
			 
		
	}
	@RequestMapping("/findRoleModule")
	@ResponseBody
	private Object findRoleModule(String roleId){
		
		logger.info("进入根据id得到模块...");
		
		List<TbRoleModule> list= roleModuleService.findModule(roleId);
		
		return list;
	}
}
